package com.start.filters;

import com.start.common.exceptions.StartBusException;
import com.start.oss.common.StartOssData;
import com.start.oss.common.filters.StartOSSFilter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class OssTypeFilter implements StartOSSFilter {
    @Override
    public void uploadBefore(StartOssData oss) {
        MultipartFile file = oss.getFile();

        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            throw new StartBusException("未知文件类型禁止上传");
        }
        if (!originalFilename.contains(".")) {
            throw new StartBusException("未知文件类型禁止上传");
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (StringUtils.isBlank(suffix)) {
            throw new StartBusException("未知文件类型禁止上传");
        }
    }


}
